﻿using System;
using System.Text;
using System.Data;
using System.Web.UI.HtmlControls;

namespace LKCMS.Web
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindMeta();
                BindIM();
                BindGN();
                BindCJ();
                BindSC();
                BindNews();
                BindLine();
                BindZX();
            }

        }



        /// <summary>
        /// 组合SQL查询语句
        /// </summary>
        /// <param name="_kindId">栏目ID</param>
        /// <param name="_classId">类别ID</param>
        /// <returns></returns>
        private string CombSqlTxt(int _kindId, int _classId)
        {
            StringBuilder strTemp = new StringBuilder();
            if (_classId > 0)
            {
                strTemp.Append(" and ClassId in(select Id from dt_Channel where KindId=" + _kindId + " and ClassList like '%," + _classId + ",%')");
            }
            return strTemp.ToString();
        }

        private void BindGN()
        {

            this.LiteralGN.Text = GetTJData("IsRed=1  " + CombSqlTxt(1, 2)).ToString();
            this.LiteralGN0.Text = GetZDData("IsTop=1  " + CombSqlTxt(1, 2)).ToString();
        }

        private void BindCJ()
        {
            this.LiteralCJ.Text = GetTJData("IsRed=1  " + CombSqlTxt(1, 3)).ToString();
            this.LiteralCJ0.Text = GetZDData("IsTop=1  " + CombSqlTxt(1, 3)).ToString();
        }

        private void BindSC()
        {
            this.LiteralSC.Text = GetTJData("IsRed=1  " + CombSqlTxt(1, 1)).ToString();
            this.LiteralSC0.Text = GetZDData("IsTop=1  " + CombSqlTxt(1, 1)).ToString();
        }

        private StringBuilder GetTJData(string where)
        {
            StringBuilder sb = new StringBuilder();
            LKCMS.BLL.Pictures pbll = new LKCMS.BLL.Pictures();
            DataSet dt = pbll.GetPageList(20, 0, where, " SortId,AddTime desc");
            if (dt != null)
            {
                foreach (DataRow dr in dt.Tables[0].Rows)
                {
                    sb.Append("<li><a href=\"DetailTW.aspx?id=" + dr["ID"].ToString() + "\" title=\"" + dr["Title"].ToString() + " - " + dr["Click"].ToString() + " hits\" target=\"_blank\">" + dr["Title"].ToString() + "</a></li>");
                }
            }
            return sb;

        }
        private StringBuilder GetZDData(string where)
        {
            StringBuilder sb = new StringBuilder();
            LKCMS.BLL.Pictures pbll = new LKCMS.BLL.Pictures();
            LKCMS.BLL.Channel cbll = new LKCMS.BLL.Channel();
            DataSet dt = pbll.GetPageList(20, 0, where, " SortId,AddTime desc");
            if (dt != null)
            {
                foreach (DataRow dr in dt.Tables[0].Rows)
                {
                    LKCMS.Model.Channel model = cbll.GetModel(cbll.GetModel(int.Parse(dr["ClassId"].ToString())).ParentId);
                    sb.Append("<li><a href=\"NormalType.aspx?id=" + model.Id + "\"><img width=\"146\" height=\"71\" alt=\"" + model.PageUrl + "\" src=\"" + dr["ImgUrl"].ToString() + "\" border=\"0\" />" + model.PageUrl + "</a></li>");
                }
            }

            return sb;

        }



        private void BindIM()
        {
            StringBuilder sb = new StringBuilder();
            LKCMS.BLL.IM bll = new LKCMS.BLL.IM();
            DataSet dt = bll.GetList(12, " IsLock=1 ", "IMType,SortId");
            if (dt != null)
            {
                    foreach (DataRow dr in dt.Tables[0].Rows)
                    {

                        string picurl = string.Empty;

                        switch (dr["IMType"].ToString())
                        {
                            case "QQ":
                                sb.Append("<a href='http://wpa.qq.com/msgrd?V=1&amp;Uin=" + dr["IMAccount"].ToString() + "&amp;Site=四川中青旅欢迎您&amp;Menu=yes' target='_blank'><img src=' http://wpa.qq.com/pa?p=1:" + dr["IMAccount"].ToString() + ":4'  border='0' alt='点击对话'  />" + dr["IMName"].ToString() + "</a>&nbsp;&nbsp;");
                                break;
                            case "MSN":
                                sb.Append(" <a href=\"msnim:chat?contact=" + dr["IMAccount"].ToString() + "\" target=blank><img src=\"images/top/msn.gif\" width=\"21\" height=\"20\" />" + dr["IMName"].ToString() + "</a>&nbsp;&nbsp;");
                                break;

                            case "淘宝旺旺":
                                sb.Append(" <a target=\"_blank\" href=\"http://amos1.taobao.com/msg.ww?v=2&uid=" + dr["IMAccount"].ToString() + "&s=1\" ><img border=\"0\" src=\"http://amos1.taobao.com/online.ww?v=2&uid=" + dr["IMAccount"].ToString() + "&s=1\" alt=\"点击这里给我发消息\" /></a>&nbsp;&nbsp;");
                                break;


                        }
                        

                    }

            }

            this.LiteralIM.Text = sb.ToString();

        }

        private string Pic(string IMName)
        {
            string picurl = string.Empty;

            switch (IMName)
            {
                case "经理":
                    picurl = "images/top/jl.gif";
                    break;
                case "国内":
                    picurl = "images/top/gn.gif";
                    break;

                case "会议":
                    picurl = "images/top/hy.gif";
                    break;

                case "出境":
                    picurl = "images/top/cj.gif";
                    break;
                default:
                    picurl = "images/top/jl.gif";
                    break;


            }
            return picurl;
        }

        private void BindNews()
        {
            StringBuilder sb = new StringBuilder();
            LKCMS.BLL.Article pbll = new LKCMS.BLL.Article();
            DataTable dt = pbll.GetPageList(8, 0, "IsTop=1  " + CombSqlTxt(0, 37), " AddTime desc").Tables[0];

            foreach (DataRow dr in dt.Rows)
            {
                string title = dr["Title"].ToString();
                if (title.Length > 16)
                {
                    title = title.Substring(0, 16) + "...";
                }

                sb.Append(" <ul>");
                sb.Append("<li><a href=\"DetailWB.aspx?id=" + dr["ID"].ToString() + "\" title=\"" + title + " target=\"_blank\">" + title + "</a></li>");
                sb.Append("</ul>");
            }

            this.LiteralNews.Text = sb.ToString();
        }

        private void BindLine()
        {
            StringBuilder sb = new StringBuilder();
            LKCMS.BLL.Pictures pbll = new LKCMS.BLL.Pictures();
            DataTable dt = pbll.GetList(8, "IsTop=1  and ( ClassId in(select Id from dt_Channel where KindId=1 and ClassList like '%,1,%') or ClassId in(select Id from dt_Channel where KindId=1 and ClassList like '%,2,%') or ClassId in(select Id from dt_Channel where KindId=1 and ClassList like '%,3,%'))", " SortId,AddTime desc").Tables[0];
            foreach (DataRow dr in dt.Rows)
            {
                string title = dr["Title"].ToString();
                if (title.Length > 16)
                {
                    title = title.Substring(0, 16) + "...";
                }

                sb.Append(" <ul>");
                sb.Append("<li><a href=\"DetailTW.aspx?id=" + dr["ID"].ToString() + "\" title=\"" + title + " target=\"_blank\">" + title + "</a></li>");
                sb.Append("</ul>");
            }

            this.LiteralLine.Text = sb.ToString();

        }

        private void BindZX()
        {

            StringBuilder sb = new StringBuilder();
            LKCMS.BLL.Feedback pbll = new LKCMS.BLL.Feedback();
            DataTable dt = pbll.GetList(8, " IsLock=1 ", " AddTime desc").Tables[0];
            foreach (DataRow dr in dt.Rows)
            {
                string title = dr["Content"].ToString();
                if (title.Length > 16)
                {
                    title = title.Substring(0, 16) + "...";
                }

                sb.Append(" <ul>");
                sb.Append("<li>" + title + "</li>");
                sb.Append("</ul>");
            }

            this.LiteralZX.Text = sb.ToString();
        }

        private void BindMeta()
        {
            LKCMS.Model.WebSet webset = new LKCMS.BLL.WebSet().loadConfig(LKCMS.Common.Utils.GetXmlMapPath("Configpath"));

            Page.Title = "首页_" + webset.WebKeywords;


            //Encode/Content type
            HtmlMeta encode = new HtmlMeta();
            encode.HttpEquiv = "Content-Type";
            encode.Content = "text/html; charset=utf-8";
            Page.Header.Controls.Add(encode);

            //Language
            HtmlMeta lang = new HtmlMeta();
            lang.HttpEquiv = "Content-Language";
            lang.Content = "zh-cn";
            Page.Header.Controls.Add(lang);

            //Description
            HtmlMeta desc = new HtmlMeta();
            desc.Name = "Description";
            desc.Content = webset.WebDescription;
            Page.Header.Controls.Add(desc);

            //Keyword
            HtmlMeta keywords = new HtmlMeta();
            keywords.Name = "keywords";
            keywords.Content = webset.WebKeywords;
            Page.Header.Controls.Add(keywords);

        }

    }


}
